package com.abe.signature;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.abe.libfile.AbeFileManager;
import com.abe.libphoto.AbeMultiMedia;
import com.abe.mvpcore.use.Ex2Activity;
import com.abe.signature.api.AppMainService;
import com.abe.signature.entity.UploadInfoModel;
import com.abe.signature.print.PrintHelper;
import com.abe.signaturesdk.OnSignatureCallBack;
import com.abe.signaturesdk.SignatureDialog;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.ValueCallback;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.Permission;

import java.io.File;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private ImageView item_image;
    private String imageUrl;
    private TextView item_txt;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        item_image = findViewById(R.id.item_image);
        item_txt = findViewById(R.id.item_txt);
        item_image.setOnClickListener(this);
        findViewById(R.id.item_button).setOnClickListener(this);
        findViewById(R.id.item_button_navi).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.item_image: {
                showSign();
//                showMedia();
            }
            break;
            case R.id.item_button: {
//                reqUploadFile(imageUrl);
//                showFile();
                printPdf();
            }
            break;
            case R.id.item_button_navi: {
//                showNavi();
                startActivity(new Intent(this, WebServiceActivity.class));
            }
            break;
        }
    }


    private void printPdf() {
        String dirPath = Environment.getExternalStorageDirectory().getAbsolutePath();
        String filePath = dirPath + File.separator + "11111111.pdf";
        PrintHelper.getInstance(this).printPDF(filePath);
    }

    private void showMvp() {
        startActivity(new Intent(MainActivity.this, Ex2Activity.class));
    }

    private void reqUploadFile(String path) {
        if (TextUtils.isEmpty(path)) return;
        File file = new File(path);
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part body = MultipartBody.Part.createFormData("aFile", file.getName(), requestFile);
        //RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), new Gson().toJson());
        Call<UploadInfoModel> call = AppMainService.getCommonService().upload(body);
        call.enqueue(new Callback<UploadInfoModel>() {
            @Override
            public void onResponse(Call<UploadInfoModel> call, Response<UploadInfoModel> response) {
                UploadInfoModel model = response.body();
                if (model == null) return;
                System.out.println(model.toString());
                if (model.getStatus().equals("1")) {
                    Toast.makeText(MainActivity.this, "上传成功", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<UploadInfoModel> call, Throwable t) {
                System.out.println(t.getMessage());
            }
        });
    }

    private void showMedia() {
        PermissionUtils.createRequest(this, new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                AbeMultiMedia.with(MainActivity.this)
                        .setType(AbeMultiMedia.TYPE_ALBUM_SELECT)
                        .setCallBack(new AbeMultiMedia.OnAbeMultiMediaCallBack() {
                            @Override
                            public void onPhoto(int type, String imagePath, final Bitmap bitmap) {
                                System.out.println("type:" + type + "\nimagePath:" + imagePath);
                                imageUrl = imagePath;
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        item_image.setImageBitmap(bitmap);
                                    }
                                });
                            }
                        }).start();
            }
        }, Permission.CAMERA, Permission.WRITE_EXTERNAL_STORAGE, Permission.READ_EXTERNAL_STORAGE).start();
    }

    private void showSign() {
        PermissionUtils.createRequest(this, new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                SignatureDialog dialog = new SignatureDialog(MainActivity.this, SignatureDialog.ThemeBlueBlack);
                dialog.setCallBackListener(new OnSignatureCallBack() {
                    @Override
                    public void onSignEnd(final Bitmap bitmap, String filePath) {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                item_image.setImageBitmap(bitmap);
                            }
                        });
                    }
                });
                dialog.show();
                dialog.setDialogWindowAttr(700, 800);
            }
        }, Permission.Group.STORAGE).start();
    }

    private void showFile() {
//        PermissionUtils.createRequest(this, new Action<List<String>>() {
//            @Override
//            public void onAction(List<String> data) {
//                AbeFileManager.with(MainActivity.this)
//                        .setType(AbeFileManager.TYPE_FILE_SD)
//                        .setListener(new AbeFileManager.OnFileChooseListener() {
//                            @Override
//                            public void onFilePath(final String path) {
//                                runOnUiThread(new Runnable() {
//                                    @Override
//                                    public void run() {
//                                        item_txt.setText(path);
//                                    }
//                                });
//                            }
//                        }).start();
//            }
//        }, Permission.Group.STORAGE).start();

        PermissionUtils.createRequest(this, new Action<List<String>>() {
            @Override
            public void onAction(List<String> data) {
                String SD_PATH = Environment.getExternalStorageDirectory().getAbsolutePath();
                String path = SD_PATH + File.separator + "G3EmlesFiles/commonTemplate.pdf";
                Bundle bundle = new Bundle();
                bundle.putString("filePath", path);
                Intent intent = new Intent(MainActivity.this, FileBrowseActivity.class);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        }, Permission.Group.STORAGE).start();
    }


    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        AbeMultiMedia.with(this).onActivityResult(requestCode, resultCode, data);
        AbeFileManager.with(this).onActivityResult(requestCode, resultCode, data);
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void onDestroy() {
        AbeMultiMedia.with(this).destroy();
        AbeFileManager.with(this).destroy();
        super.onDestroy();
    }
}